package com.igeek.service;

import com.igeek.dao.OrderDao;
import com.igeek.domain.Order;
import com.igeek.utils.DataSourceUtils;

import java.sql.SQLException;
import java.util.List;

/**
 * @author jerry
 * @Company 极客营
 */
public class OrderService {
    private OrderDao orderDao=new OrderDao();

    /**
     * 提交订单
     * @param order
     * @return
     */
    public boolean submitOrder(Order order){
        boolean flag=false;
        try {
            //开启事务
            DataSourceUtils.startTransaction();
            orderDao.insertOrder(order);
            orderDao.insertOrderItem(order);
            //提交事务
            DataSourceUtils.commitAndRelease();
            flag=true;
        } catch (SQLException e) {
            //事务回滚
            try {
                DataSourceUtils.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 查询所有订单
     */
    public List<Order> getAllOrder(String uid){
        try {
            return orderDao.findAllOrder(uid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public Order getOrderDetail(String oid){
        try {
            return orderDao.findOrderDetail(oid);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void updateOrder(Order order){
        try {
            orderDao.updateOrder(order);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void confirmOrder(Order order){
        try {
            orderDao.ConfirmOrder(order);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
